package club.baimahu.pencil.domain.session;

import club.baimahu.pencil.domain.entity.account.Account;
import club.baimahu.pencil.domain.entity.account.AccountManager;
import club.baimahu.pencil.domain.entity.account.SimpleAccountManager;
import club.baimahu.pencil.exception.BuyException;
import com.google.common.collect.Lists;
import club.baimahu.pencil.domain.entity.Goods;
import club.baimahu.pencil.domain.entity.GoodsBasket;
import club.baimahu.pencil.domain.entity.Seller;
import club.baimahu.pencil.domain.entity.location.Location;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.List;

@Data
public class Supermarket implements Seller, Location {

    Logger logger = LoggerFactory.getLogger(Supermarket.class);

    String name;

    Location location ;

    GoodsFactory goodsFactory;

    AccountManager accountManager;

    boolean sell(GoodsBasket goodsBasket) {
        return true;
    }


    public void stockWithGoods(List<GoodsStock> goodsStockList){
        if(this.goodsFactory == null){
            throw new BuyException("goodsFactory should init before stockWithGoods");
        }
        goodsFactory.stockWithGoods(goodsStockList);
    }

    public Supermarket(){}

    public Supermarket(String name , GoodsFactory goodsFactory ){
        this.name = name;
        this.goodsFactory = goodsFactory;
    }

    public AccountManager getAccountManager(){
        return this.accountManager;
    }

    public GoodsFactory getGoodsFactory(){
        return goodsFactory;
    }

    public Goods getGoods(String goodsNo){
        return goodsFactory.getGoods(goodsNo,1);
    }


    @Override
    public boolean hasGoods(String goodsNo) {
        return goodsFactory.getGoods(goodsNo,1) != null;
    }

    @Override
    public boolean sell(Goods goods,AccountManager accountManager) {
        Goods goods1 = goodsFactory.getGoods(goods.getGoodsNo(),1);
        goodsFactory.checkoutGoods(Lists.newArrayList(goods1));
        logger.info("卖出编号为{}的商品1件,售价：{}",goods.getGoodsNo(),goods.getSellPrice());

        this.accountManager.income(goods.getSellPrice(),accountManager);
        return true;
    }


    @Override
    public String getAddress() {
        return location.getAddress();
    }

    @Override
    public boolean pay(BigDecimal money, Account account) {
        return accountManager.pay(money,account);
    }

    @Override
    public boolean pay(BigDecimal money, AccountManager accountManager) {
        return false;
    }

    @Override
    public boolean income(BigDecimal money, Account account) {
        return accountManager.income(money,account);
    }

    @Override
    public boolean income(BigDecimal money, AccountManager accountManager) {
        return accountManager.income(money,accountManager.getPayAccount());
    }

    @Override
    public Account getIncomeAccount() {
        return accountManager.getIncomeAccount();
    }

    @Override
    public Account getPayAccount() {
        return accountManager.getPayAccount();
    }

    @Override
    public void addAccount(Account account) {
        if(accountManager == null){
            this.accountManager = new SimpleAccountManager(this.name,account);
            return;
        }else{
            accountManager.addAccount(account);
        }
    }

    @Override
    public String accountManagerName() {
        return accountManager.accountManagerName();
    }

    @Override
    public BigDecimal getBalance() {
        return accountManager.getBalance();
    }
}
